<template>
  <div>
    <template class="c-field" v-if="display">{{ display }}</template>
    <template v-else>
      <span class="c-field-placeholder">{{ placeholder }}</span>
    </template>
    <van-popup
      v-model="visible"
      position="bottom"
      get-container="body"
      class="c-qbcpka"
    >
      <van-cascader
        v-model="cascaderValue"
        title="请选择所在地区"
        :field-names="fieldNames"
        :options="options"
        @close="visible = false"
        @change="onChange"
        @finish="onFinish"
      />
    </van-popup>
  </div>
</template>
<script>
import API from '@/services/h5/common'
export default {
  props: {
    placeholder: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      visible: false,
      display: '',
      cascaderValue: '',
      fieldNames: {
        text: 'placeName',
        value: 'placeCode',
        children: 'children'
      },
      options: []
    }
  },
  async created() {
    this.init()
  },
  methods: {
    async init() {
      if (this.$route.query.answer == 'true') {
        return false
      }
      // 获取所在地区
      //  placeType 01-省 02-市 03-区
      let res = await API.getAreaList('01')
      if (res.data && res.data.code === 1000) {
        // this.options = res.data.data
        res.data.data.map(item => {
          this.options.push({
            ...item,
            children: []
          })
        })
      }
    },
    async onChange(option) {
      let item = option.selectedOptions[option.tabIndex]
      let res = await API.getAreaChildrenList(
        '0' + (option.tabIndex + 2),
        item.placeCode
      )
      if (res.data && res.data.code === 1000) {
        if (option.tabIndex == 0) {
          let children = []
          res.data.data.map(item => {
            children.push({
              ...item,
              children: []
            })
          })
          option.selectedOptions[option.tabIndex].children = children
        }
        if (option.tabIndex == 1) {
          option.selectedOptions[option.tabIndex].children = res.data.data
        }
      }
    },
    onFinish({ selectedOptions }) {
      this.visible = false
      this.display = selectedOptions.map(option => option.placeName).join('/')
      let value = selectedOptions.map(option => option.placeCode).join('/')
      this.$emit('confirm', value)
    }
  }
}
</script>
<style lang="less" scoped>
.c-field {
  font-size: 15px;
  color: #333;
}
.c-field-placeholder {
  color: #999;
  font-size: 15px;
}
.c-qbcpka {
  //   height: 321px;
  border-radius: 13px 13px 0 0;
  text-align: center;
  padding: 15px;
  box-sizing: border-box;
  &-title {
    font-size: 18px;
    color: #333;
    font-weight: bold;
  }
  &-confirm,
  &-cancel {
    color: @c-color-primary;
    font-size: 14px;
  }
  &-item {
    font-size: 15px;
    color: #666;
    margin-top: 15px;
    padding-bottom: 10px;
    // border-bottom: 0.5px solid #e9e9e9;
    &:last-child {
      border-bottom: none;
    }
  }
  &-item-active {
    color: @c-color-primary;
  }
}
</style>
